package gui;
import javax.swing.AbstractButton;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JSeparator;
import Entity.*;
import controllers.ManagerUIP;
import controllers.OBLSettings;
import controllers.WelcomeUIP;
import messagesPacks.*;

import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.util.Date;
import java.util.Enumeration;
import messagesPacks.*;
import javax.swing.JList;
import javax.swing.border.BevelBorder;
import javax.swing.border.MatteBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class ManagerLatenessReportUI extends JPanel
{
	private static final long serialVersionUID = 1L;
	public JButton btnLogout;
	public JButton btnExit;
	ManagerUIP muip=null; 
	public SimpleTable SubTable;
	public SimpleTable SubTable2;
	private JButton btnBack;
	private JLabel lblOblManager;
	ManagerMainUI managerMainUI=null;
	private JTextField latenessDaysAVG;
	private JTextField latenessDaysMedianR;
	private JLabel lblLatenessDaysMedianR;
	private JLabel lblLatenessDaysAVG;
	private final JSeparator separator = new JSeparator();
	private JTextField latenessDaysSumR;
	private JTextField latenessDaysCountR;
	private JLabel lblLatenessDaysSumR;
	private JLabel lblLatenessDaysCountR;
	private JLabel lblDistribution;
	JLabel lblTotalDaysDistribution;
	JRadioButton rdbtnTotalRDistribution;
	JLabel lblBookLatenessDistribution;
	JRadioButton rdbtnBookLatenessRDistribution;
	private JTextField latenessDaysMedianN;
	private JTextField latenessDaysAvgN;
	private JTextField latenessDaysSumN;
	private JTextField latenessDaysCountN;
	 JLabel lblTheLatenessDaysMedianN;
	  JLabel lblTheLatenessDaysAvgN;
	  JLabel lblTheLatenessSumN;
	  JLabel lblTheLatenessCountN;
	  JRadioButton rdbtnEnableTotalDaysN;
	  JRadioButton rdbtnBookLatenessN ;
	  public ManagerLatenessReportMSG managerLatenessReportMSG;
	public ManagerLatenessReportUI (ManagerMainUI managerMainUI) 
	{
		super();
		this.managerMainUI=managerMainUI;
		System.out.println("ManagerSuspendSub created.");
		this.muip=new ManagerUIP();
		initialize();
	}
	private void initialize()
	{
		managerLatenessReportMSG=muip.CreateLatenessReport();
		this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
		setLayout(null);
    	separator.setBounds(0, 57, 855, 30);
		add(separator);
		lblOblManager = new JLabel("OBL Manager-Lateness Report");
		lblOblManager.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblOblManager.setBounds(173, 0, 368, 58);
		add(lblOblManager);
		btnExit = new JButton("Exit");
		btnExit.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnExit.setBounds(707, 11, 83, 41);
		add(btnExit);
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnBack.setBounds(10, 11, 83, 41);
		add(btnBack);
		btnLogout = new JButton("Logout");
		btnLogout.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnLogout.setBounds(614, 11, 83, 41);
		add(btnLogout);
	    
	    latenessDaysAVG = new JTextField();
		latenessDaysAVG.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		latenessDaysAVG.setBounds(308, 69, 48, 24);
		latenessDaysAVG.setFont(new Font("Tahoma", Font.PLAIN, 15));
		add(latenessDaysAVG);
		latenessDaysAVG.setColumns(10);
		latenessDaysAVG.setEnabled(false);
		latenessDaysAVG.setText(Double.toString(managerLatenessReportMSG.avgOfAllRLateDays));
		
		latenessDaysMedianR = new JTextField();
		latenessDaysMedianR.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		latenessDaysMedianR.setBounds(308, 124, 48, 24);
		latenessDaysMedianR.setFont(new Font("Tahoma", Font.PLAIN, 15));
		add(latenessDaysMedianR);
		latenessDaysMedianR.setColumns(10);
		latenessDaysMedianR.setEnabled(false);
		latenessDaysMedianR.setText(Integer.toString(managerLatenessReportMSG.medianOfAllRLateDays));
		
		lblLatenessDaysAVG = new JLabel("The Lateness Days Of Returned Books AVG=");
		lblLatenessDaysAVG.setBounds(20, 70, 281, 24);
		lblLatenessDaysAVG.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblLatenessDaysAVG);
		
		lblLatenessDaysMedianR = new JLabel("The Median of Late days for Returned Book=");
		lblLatenessDaysMedianR.setBounds(20, 123, 291, 24);
		lblLatenessDaysMedianR.setFont(new Font("Tahoma", Font.PLAIN, 14));
		add(lblLatenessDaysMedianR);
		
		lblLatenessDaysSumR = new JLabel("The Sum Of Lateness Days for returned book=");
		lblLatenessDaysSumR.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblLatenessDaysSumR.setBounds(20, 182, 291, 24);
		add(lblLatenessDaysSumR);
		
		latenessDaysSumR = new JTextField();
		latenessDaysSumR.setFont(new Font("Tahoma", Font.PLAIN, 15));
		latenessDaysSumR.setColumns(10);
		latenessDaysSumR.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		latenessDaysSumR.setBounds(308, 183, 48, 24);
		add(latenessDaysSumR);
		latenessDaysSumR.setEnabled(false);
		latenessDaysSumR.setText(Integer.toString(managerLatenessReportMSG.sumOfAllRLateDays));
		
		lblLatenessDaysCountR = new JLabel("The Count of Lateness for Returned Books=");
		lblLatenessDaysCountR.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblLatenessDaysCountR.setBounds(20, 244, 271, 24);
		add(lblLatenessDaysCountR);
		
		latenessDaysCountR = new JTextField();
		latenessDaysCountR.setFont(new Font("Tahoma", Font.PLAIN, 15));
		latenessDaysCountR.setColumns(10);
		latenessDaysCountR.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		latenessDaysCountR.setBounds(308, 245, 48, 24);
		add(latenessDaysCountR);
		latenessDaysCountR.setEnabled(false);
		latenessDaysCountR.setText(Integer.toString(managerLatenessReportMSG.NumOfAllRLateness));
		
		lblDistribution = new JLabel(" Lateness Distribution");
		lblDistribution.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblDistribution.setBounds(290, 279, 176, 24);
		add(lblDistribution);
		
		lblTotalDaysDistribution = new JLabel("Total Lateness  Distribution :");
		lblTotalDaysDistribution.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblTotalDaysDistribution.setBounds(10, 285, 189, 24);
		add(lblTotalDaysDistribution);
		
		rdbtnTotalRDistribution = new JRadioButton("Enable Total Days Returned Books Dis");
		rdbtnTotalRDistribution.setOpaque(false);
		rdbtnTotalRDistribution.setBounds(10, 341, 207, 23);
		add(rdbtnTotalRDistribution);
		
		lblBookLatenessDistribution = new JLabel("Book Lateness  Distribution :");
		lblBookLatenessDistribution.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblBookLatenessDistribution.setBounds(573, 285, 176, 24);
		add(lblBookLatenessDistribution);
		
		rdbtnBookLatenessRDistribution = new JRadioButton("Book Lateness Returned Books Dis");
		rdbtnBookLatenessRDistribution.setOpaque(false);
		rdbtnBookLatenessRDistribution.setBounds(584, 315, 200, 23);
		add(rdbtnBookLatenessRDistribution);
	
	
	    Object ColNames[] = { "Late Days", "Repeat", "Sum"};
		SubTable2 = new SimpleTable(null, ColNames, 10, 368, 780, 198);
	    add(SubTable2.tblScrollPane,BorderLayout.CENTER);
		
		
		Object ColNames2[] = { "ISBN", "Late Days", "Repeat","Total Sum" ,"AVG","Median"};
		SubTable = new SimpleTable(null, ColNames2, 10, 368, 780, 198);
	    add(SubTable.tblScrollPane,BorderLayout.CENTER);
	    
	    lblTheLatenessDaysMedianN = new JLabel("The Median of Late days for Not Returned Books=");
	    lblTheLatenessDaysMedianN.setFont(new Font("Tahoma", Font.PLAIN, 14));
	    lblTheLatenessDaysMedianN.setBounds(408, 124, 310, 24);
	    add(lblTheLatenessDaysMedianN);
	    
	    latenessDaysMedianN = new JTextField();
	    latenessDaysMedianN.setFont(new Font("Tahoma", Font.PLAIN, 15));
	    latenessDaysMedianN.setEnabled(false);
	    latenessDaysMedianN.setColumns(10);
	    latenessDaysMedianN.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
	    latenessDaysMedianN.setBounds(725, 127, 48, 24);
	    add(latenessDaysMedianN);
	    latenessDaysMedianN.setEnabled(false);
	    latenessDaysMedianN.setText(Integer.toString(managerLatenessReportMSG.medianOfAllNLateDays));
	    
	    lblTheLatenessDaysAvgN = new JLabel("The Lateness Days Of Not Returned Books AVG=");
	    lblTheLatenessDaysAvgN.setFont(new Font("Tahoma", Font.PLAIN, 14));
	    lblTheLatenessDaysAvgN.setBounds(408, 69, 310, 24);
	    add(lblTheLatenessDaysAvgN);
	    
	    latenessDaysAvgN = new JTextField();
	    latenessDaysAvgN.setFont(new Font("Tahoma", Font.PLAIN, 15));
	    latenessDaysAvgN.setEnabled(false);
	    latenessDaysAvgN.setColumns(10);
	    latenessDaysAvgN.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
	    latenessDaysAvgN.setBounds(725, 72, 48, 24);
	    add(latenessDaysAvgN);
	    latenessDaysAvgN.setEnabled(false);
	    latenessDaysAvgN.setText(Double.toString(managerLatenessReportMSG.avgOfAllNLateDays));
	    
	    lblTheLatenessSumN = new JLabel("The Sum Of Lateness Days for Not returned Books=");
	    lblTheLatenessSumN.setFont(new Font("Tahoma", Font.PLAIN, 14));
	    lblTheLatenessSumN.setBounds(397, 182, 324, 24);
	    add(lblTheLatenessSumN);
	    
	    latenessDaysSumN = new JTextField();
	    latenessDaysSumN.setFont(new Font("Tahoma", Font.PLAIN, 15));
	    latenessDaysSumN.setEnabled(false);
	    latenessDaysSumN.setColumns(10);
	    latenessDaysSumN.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
	    latenessDaysSumN.setBounds(725, 186, 48, 24);
	    add(latenessDaysSumN);
	    latenessDaysSumN.setEnabled(false);
	    latenessDaysSumN.setText(Integer.toString(managerLatenessReportMSG.sumOfAllNLateDays));
	    
	    lblTheLatenessCountN = new JLabel("The Count of Lateness for Not Returned Books=");
	    lblTheLatenessCountN.setFont(new Font("Tahoma", Font.PLAIN, 14));
	    lblTheLatenessCountN.setBounds(408, 244, 310, 24);
	    add(lblTheLatenessCountN);
	    
	    latenessDaysCountN = new JTextField();
	    latenessDaysCountN.setFont(new Font("Tahoma", Font.PLAIN, 15));
	    latenessDaysCountN.setEnabled(false);
	    latenessDaysCountN.setColumns(10);
	    latenessDaysCountN.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
	    latenessDaysCountN.setBounds(725, 248, 48, 24);
	    add(latenessDaysCountN);
	    latenessDaysCountN.setEnabled(false);
	    latenessDaysCountN.setText(Integer.toString(managerLatenessReportMSG.NumOfAllNLateness));
	    
	    rdbtnEnableTotalDaysN = new JRadioButton("Enable Total Days Not Returned Books Dis");
	    rdbtnEnableTotalDaysN.setOpaque(false);
	    rdbtnEnableTotalDaysN.setBounds(10, 315, 227, 23);
	    add(rdbtnEnableTotalDaysN);
	    
	    rdbtnBookLatenessN = new JRadioButton("Book Lateness Not Returned Dis");
	    rdbtnBookLatenessN.setOpaque(false);
	    rdbtnBookLatenessN.setBounds(584, 341, 189, 23);
	    add(rdbtnBookLatenessN);
	    
	    JSeparator separator_1 = new JSeparator();
	    separator_1.setBounds(0, 279, 813, 8);
	    add(separator_1);
		 btnBack.addActionListener(new ActionListener() 
	     {
	     	public void actionPerformed(ActionEvent e) 
	     	{
	     		 WelcomeUI.wui.switchPanel("OBL Manager",managerMainUI ,OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);
	     	}
	     });
		 rdbtnBookLatenessN.addActionListener(new ActionListener() 
	     {
	     	public void actionPerformed(ActionEvent e) 
	     	{
	     		SubTable2.tblScrollPane.setVisible(false);
	     		revalidate();
	     		repaint();
	     		SubTable.tblScrollPane.setVisible(true);
	     		revalidate();
	     		repaint();
	     		rdbtnBookLatenessN.setSelected(true);
	     		rdbtnBookLatenessRDistribution.setSelected(false);
	     		rdbtnEnableTotalDaysN.setSelected(false);
	     		rdbtnTotalRDistribution.setSelected(false);

	    	    SubTable.model.setRowCount(0);
	    	    for (BookLatenessRecord var : managerLatenessReportMSG.BooksLatenessNArr)
	            {
	   			   SubTable.model.addRow(new Object[] {var.isbn,var.late_days,var.NumOfBookLateness,var.sumOfLateDays,	
	   					   			var.avgOfLateDays,var.medianOfLateDays});
	            }
	     	}
	     });   
		 
		 rdbtnBookLatenessRDistribution.addActionListener(new ActionListener() 
	     {
	     	public void actionPerformed(ActionEvent e) 
	     	{
	     		SubTable2.tblScrollPane.setVisible(false);
	     		revalidate();
	     		repaint();
	     		SubTable.tblScrollPane.setVisible(true);
	     		revalidate();
	     		repaint();
	     		rdbtnBookLatenessN.setSelected(false);
	     		rdbtnBookLatenessRDistribution.setSelected(true);
	     		rdbtnEnableTotalDaysN.setSelected(false);
	     		rdbtnTotalRDistribution.setSelected(false);
	    	    SubTable.model.setRowCount(0);
	    	    for (BookLatenessRecord var : managerLatenessReportMSG.BooksLatenessRArr)
	            {	
	   			     SubTable.model.addRow(new Object[] {var.isbn,var.late_days,var.NumOfBookLateness,var.sumOfLateDays,	
	   			    		 var.avgOfLateDays,var.medianOfLateDays});
	            }
	     	}
	     });   
		 
		 rdbtnEnableTotalDaysN.addActionListener(new ActionListener() 
	     {
	     	public void actionPerformed(ActionEvent e) 
	     	{
	     		SubTable.tblScrollPane.setVisible(false);
	     		revalidate();
	     		repaint();
	     		SubTable2.tblScrollPane.setVisible(true);
	     		revalidate();
	     		repaint();
	     		rdbtnBookLatenessN.setSelected(false);
	     		rdbtnBookLatenessRDistribution.setSelected(false);
	     		rdbtnEnableTotalDaysN.setSelected(true);
	     		rdbtnTotalRDistribution.setSelected(false);

	    	    SubTable2.model.setRowCount(0);
	    	    for (TotalLatenessDisRecord var : managerLatenessReportMSG.totalLatenessDisNRecord)
	            {   	
	   			   SubTable2.model.addRow(new Object[] {var.days_num,var.late_days,var.sumOfDays});
	            }
	     	}
	     });    
		 rdbtnTotalRDistribution.addActionListener(new ActionListener() 
	     {
		     	public void actionPerformed(ActionEvent e) 
		     	{
		     		SubTable.tblScrollPane.setVisible(false);
		     		revalidate();
		     		repaint();
		     		SubTable2.tblScrollPane.setVisible(true);
		     		revalidate();
		     		repaint();
		     		rdbtnBookLatenessN.setSelected(false);
		     		rdbtnBookLatenessRDistribution.setSelected(false);
		     		rdbtnEnableTotalDaysN.setSelected(false);
		     		rdbtnTotalRDistribution.setSelected(true);
		    	    SubTable2.model.setRowCount(0);
		    	    for (TotalLatenessDisRecord var : managerLatenessReportMSG.totalLatenessDisRRecord)
		            {   	
		   			SubTable2.model.addRow(new Object[] {var.days_num,var.late_days,var.sumOfDays});
		            }
		     	}
		 });     
	}
	
	public void paintComponent(Graphics g) 
	{
		g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
		
	}
}
